<?php
ini_set("soap.wsdl_cache_enabled", 0);
if( DICK_SERVICE_MODE=='lan' )
{
	ini_set('display_errors', true);
	error_reporting(E_ERROR | E_PARSE);
}
else
{
	ini_set('display_errors', false);
	error_reporting(0);
}

import('sun.util.SoapDiscovery');
import("sun.impl.LogWebService");
import('sun.util.WebServiceSecurity');

abstract class _WebServiceAction
{
	protected $_class_name;
	protected $_service_name;

	function start($class_name=null, $server_name=null)
	{
		!$class_name && $class_name   = $this->_class_name;
		!$server_name && $server_name = $this->_service_name ? $this->_service_name : $this->_class_name;
		if( !$class_name ) throw new SunException('empty _class_name, pls setup' );
		$_logstr = get_class($this)."[$class_name][$server_name]";
		if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST')
		{
			if(isset($HTTP_RAW_POST_DATA))
			{
				$request = $HTTP_RAW_POST_DATA;
			}
			else
			{
				$request = file_get_contents("php://input");
			}
			//file_put_contents('a', $request);
			$targetObj = new WebServiceSecurity($class_name);
			
			LogWebService::wLog($_logstr . " post", $request);
			$serverSoap = new SoapServer( app_url() . '&wsdl');
			//$serverSoap->setClass($class_name);
			$serverSoap->setObject($targetObj);
			$serverSoap->handle($request);
		}
		else
		{
			$validateMethod = new ReflectionMethod('WebServiceSecurity', 'validate');
			
			$disco = new SoapDiscovery($class_name, $server_name);
			$disco->addMethod($validateMethod);
			
			header("Content-type: text/xml");
			if (isset($_GET['wsdl']) )
			{
				LogWebService::wLog($_logstr . " wsdl");
				echo $disco->getWSDL();
			}
			else
			{
				LogWebService::wLog($_logstr . " dir");
				echo $disco->getDiscovery();
			}
		}
	}
}
?>